import time
import sys
import json
import io

# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')
# 创建一个新的输出流，并设置编码为UTF-8
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 将 当且路径 添加到path中
# sys.path.append(r'.')

from DrissionPage._pages.chromium_tab import ChromiumTab


def click_sale_card(tab: ChromiumTab):
    sale_card = tab.ele('#sale-card')
    sale_card.click()


def add_colors_div(tab: ChromiumTab, itemBean):
    colorBeans = itemBean['colorBeans']
    beans_size = len(colorBeans)

    struct_saleProp = tab.ele('#struct-saleProp')
    # 颜色
    color_top_ul = struct_saleProp.ele('tag:ul@@class=sell-color-item-container')
    # 颜色 右边
    add_new_span = color_top_ul.ele('tag:span@@class=next-btn-helper@@text():新增')

    color_num = 0  # 第几个颜色
    array_index = 0  # 数字下标
    for col_json in colorBeans:
        array_index = color_num
        color_num = color_num + 1
        color_name = col_json['propName']
        color_desc = col_json['propDesc']
        color_search = col_json['propSearch']

        # 颜色文字输入框 (多个)
        color_name_inputs = color_top_ul.eles('tag:input@@placeholder:主色')
        color_text_input = color_name_inputs[array_index]
        color_text_input.click()
        color_text_input.input(color_name)
        time.sleep(1)

        color_desc_inputs = color_top_ul.eles('tag:input@@placeholder:备注')
        color_desc_input = color_desc_inputs[array_index]
        color_desc_input.click()
        color_desc_input.input(color_desc)
        time.sleep(1)

        color_image_divs = color_top_ul.eles('tag:div@@class=sell-color-option-image-upload')  # 图片
        color_image_div = color_image_divs[array_index]
        color_image_div.click()
        time.sleep(1)
        search_more = array_index > 0
        from . import search_image_util
        search_image_util.search_image_input(tab, color_search, not search_more, True)

        color_top_ul.click()  # 失去焦点
        if color_num < beans_size:
            # 新增加颜色(按钮)
            add_new_span.click()


def add_sizes_div(tab: ChromiumTab, itemBean):
    sizeBeans = itemBean['sizeBeans']
    index = 0;
    beans_size = len(sizeBeans)
    struct_saleProp = tab.ele('#struct-saleProp')
    # 尺码
    size_top_ul = struct_saleProp.ele('tag:ul@@class=size-option-list')

    color_num = 0  # 第几个颜色
    array_index = 0  # 数字下标
    for sz_json in sizeBeans:
        array_index = color_num
        color_num = color_num + 1
        size_name = ''
        if 'propName' in sz_json:
            size_name = sz_json['propName']
        size_desc = ''
        if 'propDesc' in sz_json:
            size_desc = sz_json['propDesc']
        # <span class="next-input next-medium">
        # 尺码  文字输入框 (多个)
        chima_inputs = size_top_ul.eles('tag:input@@placeholder:尺码')

        # 尺码 输入框
        chima_text_input = chima_inputs[array_index]
        chima_text_input.click()
        chima_text_input.input(size_name)
        time.sleep(1)

        remark_inputs = size_top_ul.eles('tag:input@@placeholder:备注')
        # 备注 输入框
        remark_text_input = remark_inputs[array_index]
        remark_text_input.click()
        remark_text_input.input(size_desc)
        time.sleep(1)
        # 点击颜色,让输入框失去焦点
        size_top_ul.click()
        time.sleep(1)

        if color_num < beans_size:
            # 颜色 新增 (重新获取,避免错误)
            add_new_span = size_top_ul.ele('tag:span@@class=next-btn-helper@@text()^新增')
            add_new_span.click()
            time.sleep(1)


def upate_prise_count(tab: ChromiumTab, itemBean):
    itemPrise = itemBean['itemPrise']
    itemCount = itemBean['itemCount']
    struct_sku = tab.ele('#struct-sku')
    # 销售规格
    sale_type_div = struct_sku.ele('tag:span@@title:销售规格')
    sale_type_div.click()
    time.sleep(1)
    # 销售 右边

    prise_input = struct_sku.ele('tag:input@placeholder^价格')
    prise_input.click()
    time.sleep(1)
    prise_input.input(itemPrise)
    time.sleep(1)
    num_input = struct_sku.ele('tag:input@placeholder^数量')
    num_input.click()
    time.sleep(1)
    num_input.input(itemCount)
    time.sleep(1)

    batch_button = struct_sku.ele('tag:span@@text()^批量填写')
    batch_button.click()
    time.sleep(1)


def upate_outer_id_sell_time(tab: ChromiumTab, itemBean):
    itemOuterId = itemBean['itemOuterId']
    sellShelfTime = itemBean['sellShelfTime']

    # 商家编码
    outerId_div = tab.ele('#struct-outerId')
    outerId_input = outerId_div.ele('tag:input')
    outerId_input.click()
    outerId_input.input(itemOuterId)
    time.sleep(1)

    # 上架时间
    startTime_div = tab.ele('#struct-startTime')
    startTime_input = startTime_div.ele('tag:span@@class=item-label@@text()=' + sellShelfTime)
    startTime_input.click()
    time.sleep(1)



#   销售信息: 尺码, 颜色 价格
def sale_card(tab: ChromiumTab, itemBean):
    click_sale_card(tab)  # 获取焦点
    add_colors_div(tab, itemBean)  # 颜色
    add_sizes_div(tab, itemBean)  # 尺码
    upate_prise_count(tab, itemBean)  # 价格
    upate_outer_id_sell_time(tab, itemBean) # 上架时间 , 商家编码
